
package com.cmcc.pcap.packet.capture;


import com.cmcc.pcap.packet.dataObtian.ResolveDataObtain;
import com.cmcc.pcap.packet.sctp.sctp.SctpHandler;
import com.cmcc.pcap.packet.tcp.http.HttpHandler;
import com.cmcc.pcap.packet.tcp.https.HttpsHandler;
import com.cmcc.pcap.packet.tcp.tcp.TcpHandler;
import com.cmcc.pcap.packet.udp.UdpHandler;

import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @Date 2018年6月15日 上午9:12:36
 * @author Amish
 */
public class TestStart3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		useAnalysis();
		useAnalysis2();
	}

	/**
	 * 使用无返回值的解析方法
	 */
	private static void useAnalysis(){
		//analysis()方法是阻塞的，通过多线程可以实时获取已经解析得到的数据。
		AtomicBoolean flag = new AtomicBoolean(true);
		new Thread(()->{
			try {
				PacketAnalysis.analysis("1","E:\\pcap\\PCAP\\test_tcp", UdpHandler.class, HttpHandler.class, HttpsHandler.class);
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				flag.set(false);
			}
		}).start();

		//获取数据
		ResolveDataObtain resolveDataObtain = ResolveDataObtain.getResolveDataObtain();
		while (flag.get()){
			if (resolveDataObtain.haveData()){
				System.out.println(resolveDataObtain.getData().poll());
			}
		}


	}

	/**
	 * 使用有返回值的解析方法
	 */
	private static void useAnalysis2(){
		//等待解析的全部过程执行完毕，得到最终的数据
		//stack的结构为Stack<HashMap<Object,Object>>
		Queue queue = null;
		try {
//			queue = PacketAnalysis.analysis2("1","G:\\26GC", TcpHandler.class, HttpHandler.class, HttpsHandler.class);

//			queue = PacketAnalysis.analysis2("1","G:\\26GC\\pcap", SctpHandler.class);

			queue = PacketAnalysis.analysis3("1", "E:/pcap/all.pcap", SctpHandler.class);

		} catch (Exception e) {
			e.printStackTrace();
		}
		while (!queue.isEmpty()) {
			System.out.println(queue.poll());
		}
	}

}
